<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="">
      <el-form-item label="课程名" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="封面图链接" prop="coverUrl">
        <el-input
          v-model="queryParams.coverUrl"
          placeholder="请输入封面图链接"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总课时数" prop="totalLessons">
        <el-input
          v-model="queryParams.totalLessons"
          placeholder="请输入总课时数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单节课平均时长" prop="duration">
        <el-input
          v-model="queryParams.duration"
          placeholder="请输入单节课平均时长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="讲师id" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入讲师id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="精选标识" prop="featuredFlag">
        <el-input
          v-model="queryParams.featuredFlag"
          placeholder="请输入精选标识：0=普通, 1=精选"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程评分" prop="courseScore">
        <el-input
          v-model="queryParams.courseScore"
          placeholder="请输入课程评分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="扩展字段4" prop="ext4">-->
<!--        <el-input-->
<!--          v-model="queryParams.ext4"-->
<!--          placeholder="请输入扩展字段4"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="扩展字段5" prop="ext5">-->
<!--        <el-input-->
<!--          v-model="queryParams.ext5"-->
<!--          placeholder="请输入扩展字段5"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8" style="display: flex;">
      <el-col style="justify-content: left;" :span="20">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['system:userInfo:add']">新增</el-button>
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['system:userInfo:edit']">修改</el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['system:userInfo:remove']">删除</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['system:userInfo:export']">导出</el-button>
      </el-col>
      <el-col style="justify-content: right;" class="text-right" v-show="showSearch" :span="2.5">
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :span="1.5"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="coursesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程唯一id" align="center" prop="courseId" />
      <el-table-column label="课程名" align="center" prop="courseName" />
      <el-table-column label="课程简介" align="center" prop="description" />
      <el-table-column label="封面图链接" align="center" prop="coverUrl" />
      <el-table-column label="总课时数" align="center" prop="totalLessons" />
      <el-table-column label="单节课平均时长" align="center" prop="duration" />
      <el-table-column label="讲师id" align="center" prop="teacherId" />
      <el-table-column label="精选标识：0=普通, 1=精选" align="center" prop="featuredFlag" />
      <el-table-column label="课程评分" align="center" prop="courseScore" />
<!--      <el-table-column label="扩展字段4" align="center" prop="ext4" />-->
<!--      <el-table-column label="扩展字段5" align="center" prop="ext5" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:courses:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:courses:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改courses对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程名" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名" />
        </el-form-item>
        <el-form-item label="课程简介" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="封面图链接" prop="coverUrl">
          <el-input v-model="form.coverUrl" placeholder="请输入封面图链接" />
        </el-form-item>
        <el-form-item label="总课时数" prop="totalLessons">
          <el-input v-model="form.totalLessons" placeholder="请输入总课时数" />
        </el-form-item>
        <el-form-item label="单节课平均时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入单节课平均时长" />
        </el-form-item>
        <el-form-item label="讲师id" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入讲师id" />
        </el-form-item>
        <el-form-item label="精选标识" prop="featuredFlag">
          <el-input v-model="form.featuredFlag" placeholder="请输入精选标识：0=普通, 1=精选" />
        </el-form-item>
        <el-form-item label="课程评分" prop="courseScore">
          <el-input v-model="form.courseScore" placeholder="请输入课程评分" />
        </el-form-item>
<!--        <el-form-item label="扩展字段4" prop="ext4">-->
<!--          <el-input v-model="form.ext4" placeholder="请输入扩展字段4" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="扩展字段5" prop="ext5">-->
<!--          <el-input v-model="form.ext5" placeholder="请输入扩展字段5" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourses, getCourses, delCourses, addCourses, updateCourses } from "@/api/system/courses";

export default {
  name: "Courses",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // courses表格数据
      coursesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
        description: null,
        coverUrl: null,
        totalLessons: null,
        duration: null,
        teacherId: null,
        featuredFlag: null,
        courseScore: null,
        ext4: null,
        ext5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        featuredFlag: [
          { required: true, message: "精选标识：0=普通, 1=精选不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询courses列表 */
    getList() {
      this.loading = true;
      listCourses(this.queryParams).then(response => {
        this.coursesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        courseName: null,
        description: null,
        coverUrl: null,
        totalLessons: null,
        duration: null,
        teacherId: null,
        featuredFlag: null,
        courseScore: null,
        createTime: null,
        updateTime: null,
        ext4: null,
        ext5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加courses";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseId = row.courseId || this.ids
      getCourses(courseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改courses";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != null) {
            updateCourses(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourses(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids;
      this.$modal.confirm('是否确认删除courses编号为"' + courseIds + '"的数据项？').then(function() {
        return delCourses(courseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/courses/export', {
        ...this.queryParams
      }, `courses_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
